package com.rongshu.sendmessage.util.upData;

import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.bean.UpDataVersionBean;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.httpUtil.IModel;
import com.rongshu.sendmessage.view.bottomView.UpDataBottomDialog;

import java.util.ArrayList;
import java.util.List;

public class UpDataVersionActivity extends BaseActivity {

    private WebView web_view;
    private UpDataVersionBean upDate;
    private UpDataBottomDialog upDataBottomDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_data_version);
        mContext=this;
        try {
            upDate= (UpDataVersionBean) getIntent().getExtras().getSerializable("upData");
            if (upDate==null){
                upDate=new UpDataVersionBean();
                upDate.setForceUpdate(true);
                upDate.setUpdate(true);
                upDate.setAppH5Index("https://www.baidu.com");
                upDate.setAndroidAppUpdateURL("https://www.baidu.com");
            }
        }catch (Exception e){
            e.printStackTrace();
            upDate=new UpDataVersionBean();
            upDate.setForceUpdate(true);
            upDate.setUpdate(true);
            upDate.setAppH5Index("https://www.baidu.com");
            upDate.setAndroidAppUpdateURL("https://www.baidu.com");
        }
//        Intent intent=new Intent();
//        intent.putExtra("a",upDate);
        initView();
        initData(upDate.isForceUpdate()?2:1);
    }

    private void initView() {
        web_view=findViewById(R.id.web_view);
        initWebView(web_view);
    }
    private void initWebView(WebView web_view) {
        web_view.getSettings().setDefaultTextEncodingName("UTF-8");
        web_view.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                handler.proceed();// 接受所有网站的证书
            }
        });
        WebSettings webSettings = web_view.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        web_view.loadUrl(upDate.getAppH5Index());
    }


    /**
     *
     * @param i i==0加载正常，i==1加载更新，非强制更新，i=2强制更新
     */
    private void initData(int i) {
        switch (i){
            case 1:
                showUpDialog();
                break;
            case 2:

                showMustUpDialog();
                break;
            default:
                break;
        }
    }
    /**
     * 展示更新弹框----强制
     */
    private void showMustUpDialog() {
        DialogUtil.showAlertDialog(mContext,"版本更新", "您有新版本请及时更新", new DialogUtil.OnDialogListener<Void>() {
            @Override
            public void onPositiveButtonClick(Void aVoid) {
                goUpDataing(2,upDate);
            }
            @Override
            public void onNegativeButtonClick(Void aVoid) {
                finishApp();
            }
        });
    }
//state 1非强制2强制
    public void goUpDataing(int state, UpDataVersionBean upData){
        UpDataDialog upDataDialog=new UpDataDialog(state,upData);
        upDataDialog.showDialog(mContext);
    }

    /**
     * 展示更新弹框----非强制
     */
    private void showUpDialog() {
        DialogUtil.showConfirmDialog(mContext, "版本更新", "您有新版本请及时更新", new DialogUtil.OnDialogListener<Void>() {
            @Override
            public void onPositiveButtonClick(Void aVoid) {
                upDataBottomDialog = new UpDataBottomDialog(1, upDate.getAndroidAppUpdateURL());
                upDataBottomDialog.setOnclickLisner(new UpDataBottomDialog.DialogCloseLinstner() {
                    @Override
                    public void closeLinstner(int status) {
                        if (status==1){
                            finish();
                        }else if (status==10){
                            initData(1);
                        }
                    }
                });
                upDataBottomDialog.showDialog(mContext);
            }

            @Override
            public void onNegativeButtonClick(Void aVoid) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Log.d("UPDATA","onBackPressed");
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.d("UPDATA","onKeyUp");
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d("UPDATA","onKeyDown");
        // 监听返回键，点击两次退出程序
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (upDate.isForceUpdate()) {
                Toast.makeText(getApplicationContext(), "请及时更新", Toast.LENGTH_LONG).show();
                  return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}